package com.zyht.bank.wechat.intercepter;

import java.lang.reflect.Method;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.github.pagehelper.PageHelper;
import com.wx.entity.WxFansUser;
import com.wx.model.dictionary.Constant;
import com.zyht.bank.entity.AxdCustInfo;
import com.zyht.bank.entity.AxdCustInfoExample;
import com.zyht.bank.service.AxdCustInfoService;

/**
 * 验证用户是否判定
 * 
 * @author qjl
 * @data 2017年7月5日
 */
public class CustomerIntercepter extends HandlerInterceptorAdapter {

	@Resource
	private AxdCustInfoService axdCustInfoService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		if (handler instanceof HandlerMethod) {
			HttpSession session = request.getSession();
			HandlerMethod handlerMethod = (HandlerMethod) handler;
			Method method = handlerMethod.getMethod();
			Binding binding = method.getAnnotation(Binding.class);
			if (binding != null) {
				WxFansUser wxFansUser = (WxFansUser) session
						.getAttribute(Constant.CURRENT_USER_FANINFO_FROM_EVENT_VIEW);
				AxdCustInfoExample example = new AxdCustInfoExample();
				example.createCriteria().andFanUserIdEqualTo(wxFansUser.getId()).andBandingStatusEqualTo("1");
				PageHelper.startPage(1, 1);
				List<AxdCustInfo> list = axdCustInfoService.list(example);
				if (list.size() > 0) {
					session.setAttribute(Constant.CURRENT_USER_BINDINFO_FROM_EVENT_VIEW, list.get(0));
					return true;
				} else {
					request.getRequestDispatcher("/anon/cust/tobind").forward(request, response);
					return false;
				}

			}
			return true;
		} else {
			return super.preHandle(request, response, handler);
		}
	}

}
